#include "UpgradePanel.h"
#include "ResourcesManager.h"
#include "TowerManager.h"
#include "../tower/TowerType.h"
#include "CoinManager.h"

UpgradePanel::UpgradePanel()
{
    const ResourcesManager::TexturePool &texture_pool = ResourcesManager::Instance().texturePool;

    texIdle = texture_pool.find(ResID::Tex_UIUpgradeIdle)->second;
    texHoveredTop = texture_pool.find(ResID::Tex_UIUpgradeHoveredTop)->second;
    texHoveredLeft = texture_pool.find(ResID::Tex_UIUpgradeHoveredLeft)->second;
    texHoveredRight = texture_pool.find(ResID::Tex_UIUpgradeHoveredRight)->second;
}

void UpgradePanel::OnUpdate(SDL_Renderer *renderer)
{
    static TowerManager &instance = TowerManager::Instance();

    valTop = (int) instance.GetUpgradeCost(TowerType::Axeman);
    valLeft = (int) instance.GetUpgradeCost(TowerType::Archer);
    valRight = (int) instance.GetUpgradeCost(TowerType::Gunner);

    Panel::OnUpdate(renderer);
}

void UpgradePanel::OnClickTopArea()
{
    CoinManager &instance = CoinManager::Instance();

    if (valTop > 0 && valTop <= instance.GetCurrentCoinNum()) {
        TowerManager::Instance().UpgradeTower(TowerType::Axeman);
        instance.DecreaseCoin(valTop);
    }
}

void UpgradePanel::OnClickLeftArea()
{
    CoinManager &instance = CoinManager::Instance();

    if (valLeft > 0 && valLeft <= instance.GetCurrentCoinNum()) {
        TowerManager::Instance().UpgradeTower(TowerType::Axeman);
        instance.DecreaseCoin(valLeft);
    }
}

void UpgradePanel::OnClickRightArea()
{
    CoinManager &instance = CoinManager::Instance();

    if (valRight > 0 && valRight <= instance.GetCurrentCoinNum()) {
        TowerManager::Instance().UpgradeTower(TowerType::Axeman);
        instance.DecreaseCoin(valRight);
    }
}